package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.FeeRuleMapper;
import cc.rengu.igas.bomp.common.entity.FeeRule;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 手续费规则表操作接口实现类
 * Created by 王成 on 2018/10/10.
 */
public class FeeRuleMapperImpl implements FeeRuleMapper {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<FeeRule> selectFeeRuleByFeeRuleCode(String feeRuleCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFeeRuleByFeeRuleCode");
        FeeRule feeRule = new FeeRule();
        feeRule.setFeeCd(feeRuleCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, feeRule, FeeRule.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据费率规则代码<{}>手续费计费规则失败!", feeRuleCode);
            return null;
        }
        return objectList.stream().map(item -> (FeeRule) item).collect(Collectors.toList());
    }

    @Override
    public List<FeeRule> selectFeeRuleByStatus(String status, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFeeRuleByStatus");
        sqlStr = sqlStr.replaceAll("&lt;", "<");
        sqlStr = sqlStr.replaceAll("&gt;", ">");
        FeeRule feeRule = new FeeRule();
        feeRule.setImpStatus(status);
        feeRule.setStartDate(startDate);
        feeRule.setEndDate(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, feeRule, FeeRule.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据费率状态<{}>手续费计费规则失败!", status);
            return null;
        }
        return objectList.stream().map(item -> (FeeRule) item).collect(Collectors.toList());
    }
}
